﻿﻿using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

namespace MapLib.Examples
{
    public partial class MapExamples
    {
        // the handle of the layer with markers
        private int m_layerHandle = -1;

        // <summary>
        // Loads the layers and registers event handlers
        // </summary>
        public void MarkPoints(string dataPath)
        {
            if (this.AddLayersFromPath(dataPath))
            {
                Shapefile sf = new Shapefile();
                if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POINT))
                {
                    MessageBox.Show("Failed to create shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
                    return;
                }

                m_layerHandle = axMap1.AddLayer(sf, true);

                ShapeDrawingOptions options = sf.DefaultDrawingOptions;
                options.PointType = tkPointSymbolType.ptSymbolPicture;
                options.Picture = this.OpenMarker();
                sf.CollisionMode = tkCollisionMode.AllowCollisions;

                axMap1.SendMouseDown = true;
                axMap1.CursorMode = tkCursorMode.cmNone;
                axMap1.MouseDownEvent += new AxMapWinGIS._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent);   // change MapEvents to axMap1
            }
        }

        // <summary>
        // Opens a marker from the file
        // </summary>
        private Image OpenMarker()
        {
            string path = @"..\..\icons\marker.png";
            if (!File.Exists(path))
            {
                MessageBox.Show("Can't find the file: " + path);
            }
            else
            {
                Image img = new Image();
                if (!img.Open(path, ImageType.USE_FILE_EXTENSION, true, null))
                {
                    MessageBox.Show(img.get_ErrorMsg(img.LastErrorCode));
                    img.Close();
                }
                else
                    return img;
            }
            return null;
        }

        // <summary>
        // Handles mouse down event and adds the marker
        // </summary>
        public void axMap1_MouseDownEvent(object sender, _DMapEvents_MouseDownEvent e)
        {
            if (e.button == 1)          // left button
            {
                Shapefile sf = axMap1.get_Shapefile(m_layerHandle);

                Shape shp = new Shape();
                shp.Create(ShpfileType.SHP_POINT);

                Point pnt = new Point();
                double x = 0.0;
                double y = 0.0;
                axMap1.PixelToProj(e.x, e.y, ref x, ref y);
                pnt.x = x;
                pnt.y = y;
                int index = shp.numPoints;
                shp.InsertPoint(pnt, ref index);

                index = sf.NumShapes;
                if (!sf.EditInsertShape(shp, ref index))
                {
                    MessageBox.Show("Failed to insert shape: " + sf.get_ErrorMsg(sf.LastErrorCode));
                    return;
                }
                axMap1.Redraw();
            }
        }
    }

}
